package vg.shared.graph.editor;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import vg.interfaces.ll_services.data_base_service.data.graph.Attribute;
import vg.interfaces.ll_services.data_base_service.data.graph.Edge;
import vg.interfaces.ll_services.data_base_service.data.graph.Vertex;
import vg.interfaces.ll_services.data_base_service.data.header.AttributeHeader;
import vg.services.main_manager.VGMainServiceHelper;

public class SharedGraphEditor {
	// Defines
    public static final String INTEGER_ATTRIBUTE_TYPE = "integer";
    public static final String DOUBLE_ATTRIBUTE_TYPE = "double";
    public static final String BOOLEAN_ATTRIBUTE_TYPE = "boolean";
    public static final String STRING_ATTRIBUTE_TYPE = "string";

	// Swing components
	private static JComponent[] dialogComponents = null;
	private static JLabel nameLabel;
	private static JTextField nameTextField;
	private static JLabel valueLabel;
	private static JTextField valueTextField;
	private static JComboBox typeCompoBox;
	
	// Mutex
	private static final Object generalMutex = new Object();
	
	public static Attribute addAttribute4Vertex(Vertex v) {
		synchronized (generalMutex) {
			createComponents4AddAttributeDialog();
			if (JOptionPane.showConfirmDialog(null, dialogComponents, "Add attribute to vertex", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == 0) {
				String name = nameTextField.getText();
				String strValue = valueTextField.getText();
				int type = editorAttributeType2VisualGraphAttributeType((String)typeCompoBox.getSelectedItem());

                int attrId = VGMainServiceHelper.graphDataBaseManager.createVertexAttributeHeader(v.getStorableId(), name, strValue, type);

                return VGMainServiceHelper.graphDataBaseManager.getAttribute(attrId);
			}
			return null;
		}
	}

	public static Attribute addAttribute4Edge(Edge e) {
		synchronized (generalMutex) {
			createComponents4AddAttributeDialog();
			if (JOptionPane.showConfirmDialog(null, dialogComponents, "Add attribute to edge", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == 0) {
                String name = nameTextField.getText();
                String strValue = valueTextField.getText();
                int type = editorAttributeType2VisualGraphAttributeType((String)typeCompoBox.getSelectedItem());

                int attrId = VGMainServiceHelper.graphDataBaseManager.createEdgeAttributeHeader(e.getStorableId(), name, strValue, type);

                return VGMainServiceHelper.graphDataBaseManager.getAttribute(attrId);
			}
			return null;
		}
	}

//=============================================================================
//-----------------PRIVATE METHODS---------------------------------------------
	private static void createComponents4AddAttributeDialog() {
		synchronized (generalMutex) {
			if (dialogComponents == null) {
				if (!SwingUtilities.isEventDispatchThread()) {
					try {
						SwingUtilities.invokeAndWait(new Runnable() {
							@Override
							public void run() {
								synchronized (generalMutex) {
									createComponents4AddAttributeDialog();									
								}
							}
						});
					} catch (Exception ex) {
						VGMainServiceHelper.logger.printException(ex);
					}
				} else {
					nameLabel = new JLabel("Name");
					nameTextField = new JTextField();
					valueLabel = new JLabel("Value");
					valueTextField = new JTextField();
					
					Object[] types = new Object[4];
					types[0] = STRING_ATTRIBUTE_TYPE;
                    types[1] = INTEGER_ATTRIBUTE_TYPE;
                    types[2] = DOUBLE_ATTRIBUTE_TYPE;
					types[3] = BOOLEAN_ATTRIBUTE_TYPE;
					typeCompoBox = new JComboBox(types);
					
					dialogComponents = new JComponent[5];
					dialogComponents[0] = nameLabel;
					dialogComponents[1] = nameTextField;
					dialogComponents[2] = valueLabel;
					dialogComponents[3] = valueTextField;
					dialogComponents[4] = typeCompoBox;
				}
			} else {
				for (JComponent c: dialogComponents) {
					SwingUtilities.updateComponentTreeUI(c);
				}
			}
		}
	}

    private static int editorAttributeType2VisualGraphAttributeType(String type) {
        if (type == null)
            return AttributeHeader.NO_ATTRIBUTE_TYPE;

        if (type.equals(STRING_ATTRIBUTE_TYPE))
            return AttributeHeader.STRING_ATTRIBUTE_TYPE;

        if (type.equals(BOOLEAN_ATTRIBUTE_TYPE))
            return AttributeHeader.BOOLEAN_ATTRIBUTE_TYPE;

        if (type.equals(DOUBLE_ATTRIBUTE_TYPE))
            return AttributeHeader.DOUBLE_ATTRIBUTE_TYPE;

        if (type.equals(INTEGER_ATTRIBUTE_TYPE))
            return AttributeHeader.INTEGER_ATTRIBUTE_TYPE;

        return AttributeHeader.STRING_ATTRIBUTE_TYPE;
    }
}

